package com.cms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.domain.Template;
import com.cms.mapper.TemplateDao;
import com.cms.service.TemplateService;

@Service
public class TemplateServiceImpl implements TemplateService {

	//单实例恭喜的数据模板对应路径
	private HashMap<String, String> tempKeyDirMP = new HashMap();
	
	@Autowired
	private TemplateDao templateDao;

	public Template getById(Long id) {
		return templateDao.getById(id);
	}
	
	public Template getByKey(String key_word){
		return templateDao.getByKey(key_word);
	}

	public List<Template> getList(Long id_sbj) {

		return templateDao.getListByIdSbj(id_sbj);
	}
	
	public List<Template> getList(Long id_sbj, int pageNum, int pageSize){
		int startIndex = 0;
		if(pageNum>1){
			startIndex = (pageNum-1)*pageSize;
		}
		return templateDao.getListForPageByIdSbj(id_sbj, startIndex, pageSize);
	}

	public List<Template> getList(Template searchEntity) {

		return templateDao.getList(searchEntity);
	}

	public void add(Template entity) {
		entity.setCreatetime(new Date());
		templateDao.add(entity);
		this.tempKeyDirMP.put(entity.getKeyWord(), entity.getDirname());
	}

	public void delete(Template entity) {
		templateDao.delete(entity);
		this.tempKeyDirMP.remove(entity.getKeyWord());
	}

	public void update(Template entity) {
		entity.setCreatetime(new Date());
		templateDao.update(entity);
		this.tempKeyDirMP.put(entity.getKeyWord(), entity.getDirname());
	}
	
	public Integer getRowCount(Long id_sbj){
		return templateDao.getRowCount(id_sbj);
	}
	
	public String getRelativePathByKey(String key) {
		String dirname = this.tempKeyDirMP.get(key);
		if (dirname==null){
			Template temp = templateDao.getByKey(key);
			if(temp!=null){
				this.tempKeyDirMP.put(key, temp.getDirname());
				dirname = temp.getDirname();
			}else{
				return null;
			}
		}
		
		return "../".concat(dirname).concat("/");
	}
	
}
